#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#
# Batch process check existance
#

import psutil
import re
import collections


def ckpat(**map_str_pat):
    #
    # map_str_pat: patstr <> pat
    #   k := str := *.gnome.*
    #   v := pat := re.compile("*.gnome.*")
    #
    # Return:
    # dic: patstr:str <> p:process_iter
    #

    dic = collections.OrderedDict()

    for cmdline in map_str_pat.keys():
        dic[cmdline] = []

    for p in psutil.process_iter():
        cmdline = " ".join(p.cmdline())

        for k, v in map_str_pat.items():
            if v.match(cmdline):
                lis = dic.get(k)
                dic[k] = lis
                lis.append(p)

    return dic


def mkpat(pats):
    """Generate pat list from command line"""

    map_str_pat = collections.OrderedDict()

    for l in pats:
        l = l.strip()
        if l:
            map_str_pat[l] = re.compile(l)

    return map_str_pat


# vim: sw=4 ts=4 sts=4 ai et
